using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Web.Security;
using DiscMaster.Web.Models;

namespace DiscMaster.Web.Areas.MyDetails.Controllers
{
    public class MyDetailsController : Controller
    {
        private IDiscMasterRepository repository;

        public MyDetailsController()
            : this(new DiscMasterRepository()){
        }
        public MyDetailsController(IDiscMasterRepository controllerrepository)
        {
            repository = controllerrepository;
        }

        //
        // GET: /MyDetails/

        public ActionResult Index()
        {
            DiscMaster.Web.Models.Player player = repository.GetLoggedOnPlayer();
            if (player == null)
            {
                return View("NotFound");
            }
            return View("Index",player);
        }

        //
        // GET: /MyDetails/Edit/5

        [Authorize()]
        public ActionResult Edit()
        {
            DiscMaster.Web.Models.Player player = repository.GetLoggedOnPlayer();
            if (player==null)
            {
                return View("NotFound");
            }
            return View(player);
        }

        //
        // POST: /MyDetails/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(string id, FormCollection collection)
        {
            DiscMaster.Web.Models.Player player = repository.GetPlayer(id);
            try
            {
                UpdateModel(player);

                repository.Save();

                return RedirectToAction("Index");
            }
            catch
            {
                return View(player);
            }
        }
    }
}
